package com.wonderful.map.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.PointF
import android.util.AttributeSet
import androidx.core.content.ContextCompat
import com.wonderful.map.R
import com.wonderful.map.bean.GridPositionBean

/**
 * Desc: 清洁区域&路径 view
 * @Author: wonderful
 * @Time: 2023/5/30 15:57
 */
class VirtualCleanView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : BaseVirtualView(context, attrs, defStyleAttr, defStyleRes) {

    private val mPlanPathLines = mutableListOf<List<GridPositionBean>>()    // 规划路径

    private val colorCleanView = ContextCompat.getColor(context, R.color.color_clean_view)
    private val colorPlanPath = ContextCompat.getColor(context, R.color.color_plan_path)

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.let {
            drawPlanPath(it)
        }
    }

    override fun updatePaintStyle(isClicked: Boolean, type: Int) {
        mPolylinePaint.color = if (isClicked) colorClicked else colorCleanView
        mPolygonPaint.color = if (isClicked) colorClicked else colorCleanView
        mCirclePaint.color = if (isClicked) colorClicked else colorCleanView
    }

    /**
     * 绘制规划路径
     */
    private fun drawPlanPath(canvas: Canvas) {
        mPolylinePaint.color = colorPlanPath
        for (planPathLine in mPlanPathLines) {
            drawPolyline(planPathLine, canvas)
        }
    }

    /**
     * 设置规划路径
     */
    fun setPlanPathLines(
        planPathLines: List<List<GridPositionBean>>,
        vTranslate: PointF,
        scale: Float,
        mapHeight: Int
    ) {
        mPlanPathLines.clear()
//        for (positionBeans in planPathLines) {
//            val positions = mutableListOf<GridPositionBean>()
//            for (position in positionBeans) {
//                positions.add(GridPositionBean(position.x, mapHeight - position.y))
//            }
//            mPlanPathLines.add(positions)
//        }
        mPlanPathLines.addAll(planPathLines)
        refreshView(vTranslate, scale)
    }

    /**
     * 清空数据
     */
    override fun clear() {
        super.clear()
        clearPlanPath()
    }

    /**
     * 清除规划路径
     */
    fun clearPlanPath() {
        mPlanPathLines.clear()
        invalidate()
    }
}